﻿using System;
using System.Collections.Generic;
using DataBaseLibrary.DBInterfaces;
using System.ComponentModel;

namespace DataBaseLibrary.DBObjectsFB
{
    /// <summary>
    /// Транспортный объект. Application - то же самое, что и Declaration
    /// </summary>
    public class Application : DBFObject, IDBObject, INotifyPropertyChanged
    {


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }



        /// <summary>
        /// Дата создания договора
        /// </summary>
        private DateTime mDateCreateContract = DateTime.MinValue;
        public DateTime DateCreateContract
        {
            set { mDateCreateContract = value; OnPropertyChanged(new PropertyChangedEventArgs("DateCreateContract")); }
            get { return mDateCreateContract; }
        }


        String m_NumberDeclarationContract = "";
        ///Номер договора к/п. (ID)
        public String NumberDeclarationContract
        {
            get
            {
                return m_NumberDeclarationContract;
            }
            set
            {
                m_NumberDeclarationContract = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberSalesContract"));
            }
        }

        private String m_NameRegistrationDevision = "";
        public String NameRegistrationDevision
        {
            get { return m_NameRegistrationDevision; }
            set 
            {
                m_NameRegistrationDevision = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NameRegistrationDevision"));
            }
        }

        private int m_oOwner = 0;

        public Individual Owner
        {
            get;
            set;
        }
        private int m_oRepresentative = 0;

        public Representative Representative
        {
            get;
            set;
        }
        private int m_oVehicle = 0;

        public Vechicle Vehicle
        {
            get;
            set;
        }

        private int _mAction = 0;
        public int Action
        {
            get
            {
                return _mAction;
            }
            set
            {
                _mAction = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Action"));
            }
        }


        public override string ToString()
        {
            return String.Format("Заявление №{0}", NumberDeclarationContract);
        }

        public Application()
        {
            NameTable = "APPLICATION_TABLE";
            m_NameRegistrationDevision = "РЭО ГИБДД";
            Owner = new Individual();
            Representative = new Representative();
            Vehicle = new Vechicle();

            DateCreateContract = DateTime.Now;
        }

        object IDBObject.Save()
        {
            try
            {
                Parameters = new Dictionary<string, object>();
                Parameters.Add("NAME_REGISTRATION_DEVISION", m_NameRegistrationDevision);
                Parameters.Add("ID_OWNER", (Owner as IDBObject).Save());
                Parameters.Add("ID_REPRESENTATIVE", (Representative as IDBObject).Save());
                Parameters.Add("ID_VECHICLE", (Vehicle as IDBObject).Save());
                Parameters.Add("ACTION", Action);
                Parameters.Add("DATE_CREATE", DateCreateContract);
                return NumberDeclarationContract = (String)SaveObject();
            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при сохранении объекта {0} в БД", this.ToString()));
            }            
        }

        void IDBObject.Load(object ID)
        {
            Parameters = new Dictionary<string, object>();
            Parameters.Add("NAME_REGISTRATION_DEVISION", m_NameRegistrationDevision);
            Parameters.Add("ID_OWNER", m_oOwner);
            Parameters.Add("ID_REPRESENTATIVE", m_oRepresentative);
            Parameters.Add("ID_VECHICLE", m_oVehicle);
            Parameters.Add("ACTION", Action);
            Parameters.Add("DATE_CREATE", DateCreateContract);

            LoadObject(ID);
            NameRegistrationDevision = (string) Parameters["NAME_REGISTRATION_DEVISION"];
            ((Owner = new Individual()) as IDBObject).Load(Parameters["ID_OWNER"]);
            ((Representative = new Representative()) as IDBObject).Load(Parameters["ID_REPRESENTATIVE"]);
            ((Vehicle = new Vechicle()) as IDBObject).Load(Parameters["ID_VECHICLE"]);
            Action = (int)Parameters["ACTION"];
            DateCreateContract = DateTime.Parse(Parameters["DATE_CREATE"].ToString());
            NumberDeclarationContract = (String)ID;
        }


        void IDBObject.Update()
        {
            (Owner as IDBObject).Update();
            (Representative as IDBObject).Update();
            (Vehicle as IDBObject).Update();

            Parameters = new Dictionary<string, object>();
            Parameters.Add("NAME_REGISTRATION_DEVISION", m_NameRegistrationDevision);
            Parameters.Add("ACTION", Action);
            Parameters.Add("DATE_CREATE", DateCreateContract);
            UpdateObject();
        }

    }

    
}
